package com.yuanduan.mplus.view;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.overlay.DrivingRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLive;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearch;
import com.amap.api.services.weather.WeatherSearchQuery;
import com.cj.framework.app.BaseActivityWithBundle;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import static com.yuanduan.mplus.R.id.driver_iv_weatherPic;

/**
 * 导航界面
 */
public class DriverActivity extends BaseActivityWithBundle implements View.OnClickListener, RouteSearch.OnRouteSearchListener, GeocodeSearch.OnGeocodeSearchListener, WeatherSearch.OnWeatherSearchListener {
    private RelativeLayout rl_maindriver;
    private ImageView iv_back;
    private ImageView iv_nav;
    private ImageView iv_weather;
    private ImageView iv_call;
    private MapView mapView;
    private TextView tv_mount;
    private TextView tv_time;

    private ImageView iv_nv_back;
    private EditText et_nv_beginner;
    private EditText et_nv_endder;
    private TextView tv_nv_startnv;
    private LinearLayout ll_nv;

    private LinearLayout ll_weather;
    private ImageView iv_weather_back;
    private ImageView iv_weatherPic;
    private TextView tv_weatherDetail1;
    private TextView tv_weatherDetail2;
    private TextView tv_weatherDetail3;

    private String weatherDetail1;
    private String weatherDetail2;
    private String weatherDetail3;
    private WeatherSearch weatherSearch;
    private WeatherSearchQuery weatherSearchQuery;
    private DrivingRouteOverlay lastOverlay;
    private String target;
    private GeocodeQuery geocodeQuery;
    private GeocodeSearch geocodeSearch;
    private RouteSearch routeSearch;
    private ProgressDialog dialog;
    private AMap aMap;
    private IHttp iHttp;
    private Handler locationHandler;
    private Runnable locationRunnable;
    private Handler timeHandler;
    private Marker marker;
    private MarkerOptions markerOptions;
    private double latitude=29.087382;
    private double longitude=119.635879;
    private double time=0;
    private double length=0;
    private LatLng locationLatLng=new LatLng(latitude,longitude);
    private IHttp.HttpInfoCallback locationHttpInfoCallback=new IHttp.HttpInfoCallback() {
        @Override
        public void onSucceed(Object result) {
            //更新坐标字段locationLatLng
            latitude+=0.00001;
            longitude+=0.00001;
            locationLatLng=new LatLng(latitude,longitude);
            //更新地图坐标
            marker.setPosition(locationLatLng);
            aMap.moveCamera(CameraUpdateFactory.changeLatLng(locationLatLng));
        }

        @Override
        public void onFailed(Exception e) {

        }
    };
    private IHttp.HttpInfoCallback mountHttpInfoCallback=new IHttp.HttpInfoCallback() {
        @Override
        public void onSucceed(Object result) {
            //更新时长公里字段
            time+=0.1;
            length+=0.1;
            //更新textview
            tv_mount.setText(time+"h\n"+length+"公里");
        }

        @Override
        public void onFailed(Exception e) {

        }
    };


    @Override
    protected void findId() {
        ll_weather= (LinearLayout) findViewById(R.id.driver_ll_weather);
        iv_weatherPic= (ImageView) findViewById(driver_iv_weatherPic);
        iv_weather_back= (ImageView) findViewById(R.id.driver_iv_weather_back);
        tv_weatherDetail1= (TextView) findViewById(R.id.driver_tv_weatherDetail1);
        tv_weatherDetail2= (TextView) findViewById(R.id.driver_tv_weatherDetail2);
        tv_weatherDetail3= (TextView) findViewById(R.id.driver_tv_weatherDetail3);

        ll_nv= (LinearLayout) findViewById(R.id.driver_ll_nav);
        iv_nv_back= (ImageView) findViewById(R.id.driver_iv_nv_back);
        et_nv_beginner= (EditText) findViewById(R.id.driver_et_nv_beginner);
        et_nv_endder= (EditText) findViewById(R.id.driver_et_nv_ender);
        tv_nv_startnv= (TextView) findViewById(R.id.driver_tv_nv_startnav);

        rl_maindriver= (RelativeLayout) findViewById(R.id.driver_rl_maindriver);
        tv_mount= (TextView) findViewById(R.id.driver_tv_mount);
        tv_time= (TextView) findViewById(R.id.driver_tv_time);
        iv_back= (ImageView) findViewById(R.id.driver_iv_back);
        iv_call= (ImageView) findViewById(R.id.driver_iv_call);
        iv_nav= (ImageView) findViewById(R.id.driver_iv_nav);
        iv_weather= (ImageView) findViewById(R.id.driver_iv_weather);
        mapView= (MapView) findViewById(R.id.mapView);
    }

    @Override
    protected void logic() {
        iv_nav.setOnClickListener(this);
        iv_weather.setOnClickListener(this);
        iv_call.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        tv_nv_startnv.setOnClickListener(this);
        iv_nv_back.setOnClickListener(this);
        iv_weather_back.setOnClickListener(this);

        weatherSearch=new WeatherSearch(this);
        weatherSearch.setOnWeatherSearchListener(this);
        dialog=new ProgressDialog(this);
        routeSearch=new RouteSearch(this);
        routeSearch.setRouteSearchListener(this);
        geocodeSearch=new GeocodeSearch(this);
        geocodeSearch.setOnGeocodeSearchListener(this);

        iHttp=new HttpImp();
        timeHandler=new Handler();
        timeHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setTime();
                timeHandler.postDelayed(this,1000);
            }
        },500);
        locationHandler =new Handler();
        locationRunnable =new Runnable() {
            @Override
            public void run() {
                //请求定位点信息
                iHttp.httpInfoPost(Config.URL, new HashMap<String, String>(),locationHttpInfoCallback);
                iHttp.httpInfoPost(Config.URL,new HashMap<String, String>(),mountHttpInfoCallback);
                locationHandler.postDelayed(locationRunnable,2000);
            }
        };
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_driver;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.driver_iv_back:
                this.finish();
                break;
            case R.id.driver_iv_nav:
                ll_nv.setVisibility(View.VISIBLE);
                rl_maindriver.setVisibility(View.GONE);
                break;
            case R.id.driver_iv_weather:
                rl_maindriver.setVisibility(View.GONE);
                ll_weather.setVisibility(View.VISIBLE);
                //查询天气，更新字段
                //更新view
                weatherSearchQuery=new WeatherSearchQuery("金华",WeatherSearchQuery.WEATHER_TYPE_LIVE);
                weatherSearch.setQuery(weatherSearchQuery);
                weatherSearch.searchWeatherAsyn();
                break;
            case R.id.driver_iv_call:
                Intent intent=new Intent(Intent.ACTION_DIAL);
                startActivity(intent);
                break;
            case R.id.driver_iv_nv_back:
                ll_nv.setVisibility(View.GONE);
                rl_maindriver.setVisibility(View.VISIBLE);
                break;
            case R.id.driver_tv_nv_startnav:
                rl_maindriver.setVisibility(View.VISIBLE);
                ll_nv.setVisibility(View.GONE);
                //导航坐标查询
                target=et_nv_endder.getText().toString().trim();
                if(TextUtils.isEmpty(target)){
                    return;
                }
                dialog.show();
                geocodeQuery=new GeocodeQuery(target,"金华");
                geocodeSearch.getFromLocationNameAsyn(geocodeQuery);
                break;
            case R.id.driver_iv_weather_back:
                ll_weather.setVisibility(View.GONE);
                rl_maindriver.setVisibility(View.VISIBLE);
                break;
//            case R.i
        }
    }

    @Override
    protected void logicWithBundle(Bundle savedInstanceState) {
        mapView.onCreate(savedInstanceState);
        aMap=mapView.getMap();
        aMap.moveCamera(CameraUpdateFactory.zoomTo(19));
        markerOptions=new MarkerOptions();
        marker=aMap.addMarker(markerOptions);
        locationHandler.postDelayed(locationRunnable,1000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    private int year;
    private int month;
    private int day;
    private int hour;
    private int min;
    private int second;
    private String currentTime;
    private Calendar calendar;
    private void setTime(){
        calendar=Calendar.getInstance();
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH);
        day=calendar.get(Calendar.DAY_OF_MONTH)+1;
        hour=calendar.get(Calendar.HOUR_OF_DAY);
        min=calendar.get(Calendar.MINUTE);
        second=calendar.get(Calendar.SECOND);
        currentTime=year+"-"+month+"-"+day+" "+hour+":"+min+":"+second;
        Log.d("xx",currentTime);
        tv_time.setText(currentTime);
    }

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
        dialog.dismiss();
        if(lastOverlay!=null){
            lastOverlay.removeFromMap();
        }
        aMap.moveCamera(CameraUpdateFactory.zoomTo(17));
        DrivePath drivePath=driveRouteResult.getPaths().get(0);
        lastOverlay=new DrivingRouteOverlay(this,aMap,drivePath,driveRouteResult.getStartPos(),driveRouteResult.getTargetPos());
        lastOverlay.removeFromMap();
        lastOverlay.addToMap();
    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {

    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
        List<GeocodeAddress> addresses=geocodeResult.getGeocodeAddressList();
        GeocodeAddress address=addresses.get(0);
        RouteSearch.FromAndTo fromAndTo=new RouteSearch.FromAndTo(new LatLonPoint(locationLatLng.latitude,locationLatLng.longitude),address.getLatLonPoint());
        RouteSearch.DriveRouteQuery query=new RouteSearch.DriveRouteQuery(fromAndTo,RouteSearch.DrivingDefault,null,null,"");
        routeSearch.calculateDriveRouteAsyn(query);
    }

    @Override
    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {
        if(i==1000){
           if(localWeatherLiveResult!=null&&localWeatherLiveResult.getLiveResult()!=null){
               LocalWeatherLive live=localWeatherLiveResult.getLiveResult();
               weatherDetail1=live.getTemperature();
               weatherDetail2=live.getWeather()+"\n"+"湿度"+live.getHumidity()+"%";
               weatherDetail3=live.getReportTime();
               String weather=live.getWeather();
               if(weather.equals("晴")){

               }else if(weather.equals("晴")){

               }else if(weather.equals("晴")){

               }else if(weather.equals("晴")){

               }else{

               }
               tv_weatherDetail1.setText(weatherDetail1);
               tv_weatherDetail2.setText(weatherDetail2);
               tv_weatherDetail3.setText(weatherDetail3);
           }
        }
    }

    @Override
    public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {

    }
}
